﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Battleship
{
    /*
     * Class tổng quát tính nội suy theo cột trong bảng HydroStatic 
     */
    class HydrostaticInterpolate
    {
        private ExcelReader reader = new ExcelReader();

        // Dùng cho đồ thị thủy tĩnh
        private double[] X = new double[Constants.numHydro];
        private double[] Y = new double[Constants.numHydro];

        /*
         * Map lưu chỉ số của cột trong excel
         * Dùng cho bảng HYDROSTATIC_DATA_PATH
         */
        Dictionary<string, int> columnMap = new Dictionary<string, int>()
        {   
            { Constants.COL_DI, 1 }, { Constants.COL_V, 2 }, { Constants.COL_W, 3 }, 
            { Constants.COL_AW, 4 }, { Constants.COL_LW, 5 }, { Constants.COL_XB, 6 },
            { Constants.COL_ZB, 7 }, { Constants.COL_XW, 8 }, { Constants.COL_CB, 9 },
            { Constants.COL_CW, 10 }, { Constants.COL_CM, 11 }, { Constants.COL_TPC, 12 },
            { Constants.COL_MPC, 13 }, { Constants.COL_r, 14 }, { Constants.COL_R, 15 },
            { Constants.COL_ST, 16}, { Constants.COL_ZT, 17}, { Constants.COL_XT, 18}
        };

        /*
         * Hàm gán giá trị cho mảng X, Y dùng để nội suy đa thức
         * Dùng cho dãy giá trị theo cột trong bảng thủy tĩnh
         */
        public void loadHydroColumn(string columnX, string columnY)
        {
            reader.open(Constants.HYDROSTATIC_DATA_PATH);
            for (int i = 0; i < X.Length; i++)
            {
                X[i] = double.Parse(reader.getValue(columnMap[columnX], i + 2));
                Y[i] = double.Parse(reader.getValue(columnMap[columnY], i + 2));
            }
            reader.close();
        }

        /*
         * Hàm tính nội suy theo tên đại lượng trong bảng thủy tĩnh
         */
        public double hydroInterpolate(double x, string unitName)
        {
            switch (unitName)
            {
                case Constants.DI:
                    loadHydroColumn(Constants.COL_V, Constants.COL_DI);
                    break;
                case Constants.LCF:
                    loadHydroColumn(Constants.COL_V, Constants.COL_XW);
                    break;
                case Constants.LCB:
                    loadHydroColumn(Constants.COL_V, Constants.COL_XB);
                    break;
                case Constants.BML:
                    loadHydroColumn(Constants.COL_V, Constants.COL_R);
                    break;
                case Constants.r:
                    loadHydroColumn(Constants.COL_V, Constants.COL_r);
                    break;
                case Constants.CB:
                    loadHydroColumn(Constants.COL_V, Constants.COL_CB);
                    break;
                case Constants.KB:
                    loadHydroColumn(Constants.COL_V, Constants.COL_ZB);
                    break;
                case Constants.BM:
                    loadHydroColumn(Constants.COL_V, Constants.COL_r);
                    break;
                case Constants.ST:
                    loadHydroColumn(Constants.COL_V, Constants.COL_ST);
                    break;
                case Constants.ZT:
                    loadHydroColumn(Constants.COL_V, Constants.COL_ZT);
                    break;
                case Constants.XT:
                    loadHydroColumn(Constants.COL_V, Constants.COL_XT);
                    break;
                case Constants.MTRIM:
                    loadHydroColumn(Constants.COL_V, Constants.COL_MPC);
                     break;
                case Constants.TPC:
                    loadHydroColumn(Constants.COL_V, Constants.COL_TPC);
                    break;
                default:
                    return 0;
            }
            return Formulas.Lagrange(x, X, Y);
        }
    }
}
